package scut.kent.shopping.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import scut.kent.shopping.dao.DaoFactory;
import scut.kent.shopping.idao.ICategoryDao;
import scut.kent.shopping.idao.ICommentDao;
import scut.kent.shopping.idao.IItemDao;
import scut.kent.shopping.idao.IReplyDao;
import scut.kent.shopping.model.Category;
import scut.kent.shopping.model.Item;
import scut.kent.shopping.util.RequestUtil;

public class ItemServlet extends BaseServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4452693855264557789L;
	private IItemDao itemDao = DaoFactory.getItemDao();
	private ICommentDao commentDao=null;
	private IReplyDao replyDao=null;
	private ICategoryDao cateDao=DaoFactory.getCategoryDao();
	
	public String addInput(HttpServletRequest req,HttpServletResponse resp){
		List<Category>cates=cateDao.list();
		req.setAttribute("cs", cates);
		return "Admin/Item/addInput.jsp";
	}
	
	
	public String add(HttpServletRequest req,HttpServletResponse resp){
		Item item=(Item)RequestUtil.getObject(Item.class, req);
System.out.println(item.toString());
		itemDao.add(item);
		
		return "redirect:item.do?method=list";
	}
	
	public String delete(HttpServletRequest req,HttpServletResponse resp){
		int id = Integer.parseInt(req.getParameter("id"));
		itemDao.delete(id);
		
		return "redirect:item.do?method=list";
	}
	
	public String list(HttpServletRequest req,HttpServletResponse resp){
		List<Item> items=itemDao.list();
		req.setAttribute("items", items);
		
		return "Admin/Item/list.jsp";
	}
	
	public String updateInput(HttpServletRequest req,HttpServletResponse resp){
		int id = Integer.parseInt(req.getParameter("id"));
		
		Item item = itemDao.load(id);
		List<Category> cs= cateDao.list();
		req.setAttribute("item", item);
		req.setAttribute("cs", cs);
		return "Admin/Item/updateInput.jsp";
	}
	
	public String update(HttpServletRequest req,HttpServletResponse resp){
		Item item = (Item)RequestUtil.getObject(Item.class, req);
		itemDao.update(item);
		
		return "redirect:item.do?method=list";
	}
	
	public String show(HttpServletRequest req,HttpServletResponse resp){
		int id = Integer.parseInt(req.getParameter("id"));
		Item item = itemDao.load(id);
		req.setAttribute("item", item);
		
		return "Admin/Item/show.jsp";
	}

}
